//
// Created by Langingsing on 2021/10/26.
//

class A
{
public:
    A() : x(0)
    {}

    int x;
};

class B
{
private:
    int _x;
public:
    B() : _x(0)
    {}

    int &x()
    {
        return _x;
    }
};

class Array
{
private:
    int *_ptr; // 私有成员

public:
    explicit Array(int n) : _ptr(new int[n])
    {}

    ~Array()
    { delete[]_ptr; }

    int &operator[](int i) // 以[]来代理_ptr
    {
        return _ptr[i];
    }
};

int main()
{
    A a;
    a.x = 3;
    cout << a.x << endl;

    B b;
    b.x() = 4;
    cout << b.x() << endl;

    const int originalArray[] = {0, 1};
    originalArray[0];
    Array arr(2);

    arr[0] = 10; // !!!
    arr[1] = 11; // 写法与直接操作数组无异

    cout << arr[0] << ' ' << arr[1] << endl; // 10 11
    return 0;
}